// #include "activitymonitor.h"
// #include "ui_activitymonitor.h"

// ActivityMonitor::ActivityMonitor(QWidget *parent)
//     : QMainWindow(parent)
//     , ui(new Ui::ActivityMonitor)
// {
//     ui->setupUi(this);
// }

// ActivityMonitor::~ActivityMonitor()
// {
//     delete ui;
// }
// activitymonitor.cpp
#include "activitymonitor.h"
#include "ui_activitymonitor.h"

#include <QTimer>
#include <QScreen>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QCursor>
#include <QDateTime>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>

ActivityMonitor::ActivityMonitor(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::ActivityMonitor),
    mouseMoved(false), keyPressed(false), screenChanged(false),
    logFile("activity_log.txt"), logStream(&logFile)
{
    ui->setupUi(this);
    setWindowTitle("用户活动监视器");

    // 设置文件路径到桌面
    QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString filePath  = desktopPath + "/activity_log.txt";
    logFile.setFileName(filePath);


    // 检查UI中是否存在这些控件
    if (ui->labelMouseStatus) {
        ui->labelMouseStatus->setText("鼠标状态: 未移动");
    }
    if (ui->labelKeyStatus) {
        ui->labelKeyStatus->setText("键盘状态: 无输入");
    }
    if (ui->labelScreenStatus) {
        ui->labelScreenStatus->setText("屏幕状态: 无变化");
    }

    // 确保日志文件目录存在
    if (!QDir().mkpath(".")) {
        if (ui->labelMouseStatus) {
            ui->labelMouseStatus->setText("日志: 无法创建目录");
        }
    }

    // 打开日志文件进行追加写入
    if (logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
        // Qt6 中设置 UTF-8 编码的正确方式
        //logStream.setEncoding(QStringConverter::Utf8);

        // 写入启动日志
        logStream << "[" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
                  << "] 监视器启动" << Qt::endl;
        logStream.flush();
    } else {
        if (ui->labelMouseStatus) {
            ui->labelMouseStatus->setText("日志: 打开失败");
        }
    }

    setMouseTracking(true);

    lastMousePos = QCursor::pos();
    lastScreenGeometry = getCombinedScreenGeometry();

    monitorTimer = new QTimer(this);
    connect(monitorTimer, &QTimer::timeout, this, &ActivityMonitor::checkSystemStatus);
    monitorTimer->start(500);
}

ActivityMonitor::~ActivityMonitor()
{
    if (logFile.isOpen()) {
        logStream << "[" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
        << "] 监视器关闭" << Qt::endl;
        logStream.flush();
        logFile.close();
    }
    delete ui;
}

void ActivityMonitor::mouseMoveEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if (!event) return;
    mouseMoved = true;
    if (ui->labelMouseStatus) {
        ui->labelMouseStatus->setText("鼠标状态: 已移动");
    }
    logEvent("鼠标移动到: (" + QString::number(event->pos().x()) + ", " +
             QString::number(event->pos().y()) + ")");
}

void ActivityMonitor::keyPressEvent(QKeyEvent *event)
{
    keyPressed = true;
    if (ui->labelKeyStatus) {
        ui->labelKeyStatus->setText("键盘状态: 已敲击");
    }
    logEvent("键盘按键: " + event->text() + " (Key: " + QString::number(event->key()) + ")");
}

QRect ActivityMonitor::getCombinedScreenGeometry()
{
    QRect combined;
    for (QScreen *screen : QGuiApplication::screens()) {
        combined = combined.united(screen->geometry());
    }
    return combined;
}

void ActivityMonitor::checkSystemStatus()
{
    QPoint currentMousePos = QCursor::pos();
    if (currentMousePos != lastMousePos) {
        lastMousePos = currentMousePos;
        mouseMoved = true;
        if (ui->labelMouseStatus) {
            ui->labelMouseStatus->setText("鼠标状态: 已移动");
        }
        logEvent("鼠标位置更新: (" + QString::number(currentMousePos.x()) +
                 ", " + QString::number(currentMousePos.y()) + ")");
    }

    QRect currentGeometry = getCombinedScreenGeometry();
    if (currentGeometry != lastScreenGeometry) {
        lastScreenGeometry = currentGeometry;
        screenChanged = true;
        if (ui->labelScreenStatus) {
            ui->labelScreenStatus->setText("屏幕状态: 已改变");
        }
        // 使用 QRect 的成员变量来构建字符串
        logEvent("屏幕配置改变: x=" + QString::number(currentGeometry.x()) +
                 ", y=" + QString::number(currentGeometry.y()) +
                 ", width=" + QString::number(currentGeometry.width()) +
                 ", height=" + QString::number(currentGeometry.height()));
    }
}

void ActivityMonitor::logEvent(const QString &event)
{
    if (logFile.isOpen()) {
        logStream << "[" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
        << "] " << event << Qt::endl;
        logStream.flush(); // 立即写入文件
    }
}
